package com.edu.project.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.edu.project.R;
import com.edu.project.bean.User;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by sheng on 2016/11/5.
 */
public class UserListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ITEM=0;
    private static final int TYPE_FOOTER=1;

    private Context mContext;

    private List<User> mUserList;

    private boolean mShowFooter=true;


    public UserListAdapter(Context mContext, List<User> mUserList) {
        this.mContext = mContext;
        this.mUserList = mUserList;
    }

    @Override
    public int getItemCount() {
        int begin= mShowFooter ? 1 : 0;
        if(mUserList == null){
            return begin;
        }else {
            return mUserList.size()+begin;
        }
        //return mUserList == null ? 0 : mUserList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(!mShowFooter){
            return TYPE_ITEM;
        }
        if(position+1==getItemCount()){//到了底部
            return TYPE_FOOTER;
        }else{
            return TYPE_ITEM;
        }
    }

    //根据传递过来的值 控制是否显示加载更多布局
    public void isShowFooter(boolean b){
        this.mShowFooter=b;
    }
    public boolean isShowFooter(){
        return this.mShowFooter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case TYPE_ITEM :
                View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
                viewHolder = new UserViewHolder(convertView);
                break;
            case TYPE_FOOTER:
                View footView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer, parent, false);
                viewHolder = new FooterViewHolder(footView);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof UserViewHolder ) {
            UserViewHolder userViewHolder = (UserViewHolder) holder;
            User user = mUserList.get(position);

            Glide.with(mContext).load(user.getAvatar())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.photo_loading)
                    .into(userViewHolder.avater);
            userViewHolder.name.setText(user.getName());
            userViewHolder.age.setText(String.valueOf(user.getAge()));
            userViewHolder.sex.setText(user.getGender());
        }
    }

    class UserViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.avatar)
        ImageView avater;

        @Bind(R.id.name)
        TextView name;

        @Bind(R.id.sex)
        TextView sex;

        @Bind(R.id.age)
        TextView age;

        public UserViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }



}
